﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;

using RaceList.activity.help;
using RaceList.activity.util;

namespace RaceList.activity.relay
{
    /// <summary>
    ///     ?relay=1234(唯一的参数)
    /// </summary>
    public partial class Relayresult : Page, IAuthWeixin
    {
        public Activity_Relay Relay { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            string relayId = Request.QueryString[Parameters.relay];

            if (string.IsNullOrEmpty(relayId))
            {
                Response.Redirect404(Request.Url.ToString());
            }

            #region

            Activity_Relay relay = RelayQuery.GetRelay(Convert.ToInt32(relayId));
            if (relay != null)
            {
                SessionHelp.CycleId = relay.CycleId;
                Relay = relay;
            }
            else
            {
                Response.Redirect404(Request.Url.ToString());
            }

            #endregion

            InitProperty();
        }

        protected void BestScore_Click(object sender, EventArgs e)
        {
            if (LoginUserData.IsPlay)
            {
                int loginwxid = SessionHelp.UserData.WeixinUserId;
                ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
                //总成绩
                List<Activity_Relay> list = db.Activity_Relay.Where(s => s.WeixinUserId == loginwxid).Where(
                    s => s.CycleId == Relay.CycleId).OrderByDescending(s => s.SumScore).ToList();

                int relayId = 0;
                if (list.Count > 1)
                {
                    //判断成绩是否在前10名中
                    IEnumerable<int> relayIdTop10 = Paihangs.Select(s => s.RelayId);
                    if (relayIdTop10.Contains(list[0].Id))
                        relayId = list[1].Id;
                    else
                        relayId = list[0].Id;
                }
                else
                {
                    relayId = list[0].Id;
                }

                string url = "Relayresult.aspx?" + Parameters.relay + "=" + relayId;
                Response.Redirect(url, true);
            }
        }
    }
}